﻿using System;
using System.Threading;
using RoMBot.MemoryLib;

namespace RoMBot.Classes
{
    public class InventoryItem : Item
    {
        public InventoryItem() { }

        public InventoryItem( int slotNumber )
            : base( slotNumber )
        {
            this.SlotNumber = slotNumber;
            this.Location = "inventory";
            if ( slotNumber != 0 )
            {
                this.Update();
            }
        }

        sealed public override void Update()
        {
            var oldBagId = this.BagId;

            if ( this.SlotNumber > 60 )
            { // Is Bag Item
                this.BagId = Memory.ReadUByte( Addresses.inventoryBagIds + this.SlotNumber - 1 ) + 1;
            }
            else
            {
                this.BagId = this.SlotNumber;
            }

            if ( this.BagId != oldBagId )
            { // need new address
                this.Address = Addresses.staticInventory + ( ( this.BagId - 61 ) * 68 );
            }

            // Check if ! rented
            if ( this.BagId > 120 )
            {
                this.Available = Memory.ReadUInt( Addresses.rentBagBase + ( int ) Math.Floor( ( double ) ( this.BagId - 121 ) / 30 ) * 4 ) != 0xFFFFFFFF;
            }
            else
            {
                this.Available = true;
            }

            if ( !this.Available )
            {
                return;
            }

            base.Update();
        }

        public int Use()
        {
            var canUse = true;
            var reason = "";
            this.Update();

            if ( !this.Available || this.Empty )
            {
                return 0;
            }

            // If the item can't be used now we should be able to set a timer || something like that to recall this function && check again+.
            if ( !this.InUse )
            {
                var cd = this.GetRemainingCooldown();
                if ( cd > 0 )
                { // Item is on CoolDown we can't use it
                    //if ( this.CoolDownTime > 0 && this.LastTimeUsed != 0 &&
                    //( deltaTime( getTime(), this.LastTimeUsed ) / 1000 ) < this.CoolDownTime ) { // Item is on CoolDown we can't use it
                    canUse = false;
                    reason = "Cooldown";
                }
            }
            else
            { // Item is in use, locked, we can't use it
                reason = "In use";
                canUse = false;
            }

            if ( canUse )
            {
                Bot.Printf( "DEBUG - About to use item {0}\t BagId: {1}\t ItemCount: {2}", this.Name, this.BagId, this.ItemCount );
                Helpers.RoMScript( "UseBagItem(" + this.BagId + ")" );
                this.LastTimeUsed = DateTime.Now;
                Thread.Sleep( 500 ); // give time for server to respond with new item count
            }
            else
            {
                Bot.Printf( "DEBUG - Cannot use Item {0}\t BagId: {1} ItemCount: {2}\treason: {3}", this.Name, this.BagId, this.ItemCount, reason );
                //cprintf( cli.yellow, "DEBUG - Cannot use Item %s\t BagId: #%s ItemCount: %s\treason: %s\n", this.Name, this.BagId, this.ItemCount, reason );
                //logMessage( sprintf( "DEBUG - Cannot use Item %s\t BagId: #%s ItemCount: %s\treason: %s\n", this.Name, this.BagId, this.ItemCount, reason ) );
            }

            this.Update();

            return this.ItemCount;
        }
    }
}
